﻿$(function () {
    $("#dictype-grid").datagrid({
        url: $.Api("Sys/DicType/Grid"),
        nowrap: false, rownumbers: true, border: false,
        fit: true, singleSelect: true,
        idField: "ID", toolbar: "#dictype-tools",
        columns:
        [[
            { field: "Name", title: "名称", width: 154, align: "center" }
        ]],
        onSelect: function (index, row) {
            $("#dictionary-grid").treegrid("unselectAll");
            $("#dictionary-grid").treegrid("load", { dic: row.ID });
        }
    });
});

var DicType = {
    //添加
    Insert: function () {
        DicType.Mutual("添加", "sys/dictype/Edit.aspx");
    },
    //修改
    Update: function () {
        var row = $("#dictype-grid").datagrid("getSelected");
        if (row) {
            DicType.Mutual("修改", "sys/dictype/Edit.aspx?id=" + row.ID);
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },
    //删除
    Delete: function () {
        var row = $("#dictype-grid").datagrid("getSelected");
        if (row) {
            $.EasyuiConfirm("确认删除?", function (del) {
                if (del) {
                    $.AjaxMask({
                        url: $.Api("Sys/DicType/Delete"),
                        type: "post", data: { id: row.ID },
                        success: function (result) {
                            result = JSON.parse(result);
                            $.EasyuiSlide(result.Message);
                            if (result.Success) { DicType.Reload(); }
                        }
                    });
                }
            });
        }
        else { $.EasyuiSlide(delnullrowmsg); }
    },
    //实体操作
    Mutual: function (title, href) {
        $.ModelDialog({
            title: title, href: href, width: 260,
            handler: function () {
                $.ModelForm({
                    validate: true, button: $(this),
                    id: "#dictype-model",
                    url: $.Api("Sys/DicType/Save"),
                    success: function (result) { DicType.Reload(); }
                });
            },
            onLoad: function () { DicType.FormInit(); }
        });
    },
    //弹窗初始化
    FormInit: function () {
        $("#dictype_intsort").numberspinner({ width: 150 });
    },

    //刷新列表
    Reload: function () {
        $.DataGridReload("#dictype-grid");
        $("#dictionary-grid").treegrid("unselectAll");
        $("#dictionary-grid").treegrid("loadData", []);
    }
};